define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/font-awesome-4.7.0/css/font-awesome.min.css");
	var ajax = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var backDlg = require("resource/js/component/backDialog");
	var alertMsg = require("resource/js/component/alerts.js");
	
	var createGroupInit = function(baseUrl,row){ 
		backDlg.openBackDialog({
			href:BASEPATH+"/view/oa/approval/group/edit",
			title:'新建分组',
			backLoadFun: function() { 
				$('#formviewdatagrid').datagrid("reload", {groupName:$('#groupName').textbox('getValue')});
			}
		});

	};
	var updateGroupInit = function(baseUrl,row){ 
		backDlg.openBackDialog({
			href:BASEPATH+"/view/oa/approval/group/edit?groupId="+row.groupId,
			title:'修改分组',
			backLoadFun: function() { 
				$('#formviewdatagrid').datagrid("reload", {groupName:$('#groupName').textbox('getValue')});
			}
		});

	};
	var deleteGroup = function(baseUrl,row){
		 $.messager.confirm('系统提示','您确定要删除该组吗？',function(r){    
		    if (r){    
		    	ajax.init().success(function(code,msg,data){
	    			 if(code==0){
	    				 alertMsg.show(msg);
	    				 $('#formviewdatagrid').datagrid("reload", {groupName:$('#groupName').textbox('getValue')});
	    			 }
    			}).post(baseUrl,{groupId:row.groupId});
		    }    
		});
	}
	var saveOrder = function(baseUrl){
		var data = $("#formviewdatagrid").datagrid("getData");
		var saveArr = '';
		$.each(data.rows,function(i,val){
			saveArr = saveArr+val.groupId + ',';
		});
		saveArr = saveArr.substring(0,saveArr.length-1);
		ajax.init().success(function(code,msg,data){
			if(code==0){
				 alertMsg.show(msg);
				$('#formviewdatagrid').datagrid("reload", {groupName:$('#groupName').textbox('getValue')});
			}
		}).post(baseUrl,{groupId:saveArr});
	};

	var conf = {
		columns:[[
		          {field:"check",checkbox:true},
		          {field:'groupName',title:'分组名称',width:100,align:'center',height:50,formatter:function(value,row,index){
							return "<xmp>"+(value||'')+"</xmp>";
		        	}},
		          {field:'groupDes',title:'分组描述',width:100,align:'center',height:50,formatter:function(value,row,index){
							return "<xmp>"+(value||'')+"</xmp>";
		        	}},
		          {field:'createUserName',title:'创建人',width:100,align:'center',height:50,formatter:function(value,row,index){
		        	  value = row.createUser == 'sys'?'系统':value
		        	  return "<xmp>"+(value||'')+"</xmp>";
		        	}},
		          {field:'createTime',title:'创建时间',width:100,align:'center',height:50},
		          {field:'status',title:'使用中',width:100,align:'center',height:50,formatter:function(value,row,index){
			        	var text = value==0?'是':'否';
			        	return text;
		            }}
			    ]]
	}
	module.exports={
		init:function(){
		using(["datagrid","parser","droppable","draggable","textbox"],function(){
			$('#groupName').textbox();
			var buttons ={ 
					title:{
						_class:"",
						text:"分组管理（上下拖动可进行排序）"
					},
					btn:[
					     {
					    	 iconCls: 'icon-search',
					    	 url:'/oa/act/processGroup/queryGroupByDept.htm',
					    	 text:"查询",
					    	 addClass:'l-btn-display',
					    	 handler: function(){
					    		 $('#formviewdatagrid').datagrid("reload", {groupName:$('#groupName').textbox('getValue')});
					    	 }
					     },
					     {
					    	 iconCls: 'icon-add',
					    	 addClass:'l-btn-display',
					    	 text:"新建",
					    	 url:'/view/oa/approval/group/edit',
					    	 handler:createGroupInit
					     },
					     {
					    	 iconCls: 'icon-icon1_32',
					    	 addClass:'l-btn-display',
					    	 text:"修改",
					    	 url:'/view/oa/approval/group/edit',
					    	 needRow:true,
					    	 handler:updateGroupInit
					     },
					     {
					    	 iconCls: 'icon-remove',
					    	 addClass:'l-btn-display',
					    	 text:"删除",
					    	 url:'/oa/act/processGroup/deleteDefinedGroup.htm',
					    	 needRow:true,
					    	 handler:function(baseUrl,row){
					    		 deleteGroup(baseUrl,row);
					    	 }
					     },			     
					     {
					    	 iconCls: 'icon-icon1_19',
					    	 addClass:'l-btn-display',
					    	 text:"保存排序",
					    	 url:'/oa/act/processGroup/updateGroupOrder.htm',
					    	 handler:saveOrder
					     },
					     ]};
				var datagridconfig = {
						fit: true,
						check:true,
						fitColumns:true,
		                rownumbers:true,
						pagination:false,
						singleSelect:true,
						url:BASEPATH+"/oa/act/processGroup/queryGroupByDept.htm",
						toolbar:buttons,
						columns:conf.columns,
						eid:"formviewdatagrid",
						queryParams:{groupName:$('#groupName').textbox('getValue')},
						loadFilter:function(data){
							return data.data;
						},
						onSelect:function(index, row){
							var btns = $(".datagrid-toolbar").find("a");
							if(row.defined == '1'){
								btns[3] && $(btns[3]).linkbutton('disable');
							}else{
								btns[3] && $(btns[3]).linkbutton('enable');
							}
						},
						onLoadSuccess:function(){
							$(this).datagrid('enableDnd');
						},
				};
				seajs.use("act/approval/progress/js/datagrid-dnd.js", function() {
					$('#formviewdatagrid').datagrid(datagridconfig);
					EUtil.showBTN(undefined,datagridconfig);
				});
			});
		}
	}
});

